﻿/**
*获取请求路径
*/
  var public_url;
$(function(){
   public_url  =  basePath() + "/mobileservlet";
})


/**
 * 
 * 取消easyui默认开启的parser
 * 
 * 在页面加载之前，先开启一个进度条
 * 
 * 然后在页面所有easyui组件渲染完毕后，关闭进度条 * 
 */
 
$.parser.auto = false;
$(function() {
	$.messager.progress({
		text : '页面加载中....',
		interval : 1000
	});
	$.parser.parse(window.document);
	window.setTimeout(function() {
		$.messager.progress('close');
		if (self != parent) {
			window.setTimeout(function() {
				try {
					parent.$.messager.progress('close');
				} catch (e) {
				}
			}, 500);
		}
	}, 1);
	$.parser.auto = true;
});

/**
 * 使panel和datagrid在加载时提示
 * 
 */
$.fn.panel.defaults.loadingMessage = '加载中....';
$.fn.datagrid.defaults.loadMsg = '加载中....';

/**
 * 
 * 获得项目根路径 * @returns 项目根路径 */
basePath = function() {
	var curWwwPath = window.document.location.href;
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	var localhostPaht = curWwwPath.substring(0, pos);
	var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
	return (localhostPaht + projectName);
};

/**
 * 
 * @returns 项目名称(/oval)
 */
projectName = function() {
	return window.document.location.pathname.substring(0, window.document.location.pathname.indexOf('\/', 1));
};



/**
 * 
 * @returns 拼接参数
 */
function getJsonData(variable,value){
    var jsonData ;
    jsonData =  '{"isAsyncRequest":true,';
    for(var i = 0;i < variable.length; i++)
    {
	     if(variable[i] == 'params'){
		    jsonData +='"'+variable[i]+'":"' + value[i]+'",';
		 }else{
		     jsonData += '"'+variable[i]+'":"' + value[i]+'",';  
		 }		
    }
	jsonData += '}';
    jsonData = jsonData.replace(',}','}');
    return jsonData;
};

/**
 * 
 * 连接服务器Servlet公用方法
 */
function connectionServlet(dataStr, callbackSuccessFn) {
    $.ajax({
        type : "POST",
        url :  public_url,
        data : $.parseJSON(dataStr),
        dataType : "json",
        success : callbackSuccessFn,
        error : errorReturnData,
        beforeSend: function(xhr) {        
              //xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        },
        complete: function(xhr){}
    });
};



function errorReturnData(xhr, textStatus, errorThrown){
     alert('error------');
};




/**
 * 取得年月日(2012-11-26)
 * @return {String}
 */
function getDate(){
     var date = new Date();
     var year = date.getFullYear();
     var month = date.getMonth() + 1;
     if(month<10){
         month = "0"+month;
     }
     var day = date.getDate();
	 if( day < 10 ){
	    day = "0"+day;
	 }
    return year + "-" + month + "-" + day;
};

/**
 * 取得时分秒(08:30:30)
 *h 表示时间，m表示分钟，s表示秒
 * @return {String}
 */
 function getHMStime(h,m,s){
            var date_time = new Date();
            var timeStr="";
            var hour = date_time.getHours();
            if(hour<10){
                hour = "0"+hour;
            }
            var minute = date_time.getMinutes();
            if(minute<10){
                minute = "0"+minute;
            }
            var secode = date_time.getSeconds();
            if(secode<10){
                secode = "0"+secode;
            }
            if(h==='h'){
                timeStr += hour;
            }
            if(m==='m'){
                timeStr += ":"+minute;
            }
            if(s==='s'){
                timeStr +=":"+secode;
            }
            return timeStr;
        };

/**
 * 取得星期
 * @return {String}
 */
function getWeek(datetemp){
    var date;
    if(datetemp == null){
	     date = new Date();
	}else{
	     date = datetemp;
	}
    var weekStr = "星期";
    var weekValue = date.getDay();
    if(weekValue == 0){
        weekStr += "日";
    }else if(weekValue == 1){
        weekStr += "一";
    }else if(weekValue == 2){
        weekStr += "二";
    }else if(weekValue == 3){
        weekStr += "三";
    }else if(weekValue == 4){
        weekStr += "四";
    }else if(weekValue == 5){
        weekStr += "五";
    }else{
        weekStr += "六";
    }
    return weekStr;
};

//格式时间的Method
	/**
	* date 时间 ，split 分隔符 例：2012-11-20 默认2012年11月20日  
	*/
	function formatCalendarDate(date, split) {
		var strdate;
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		if (month < 10) {
			month = '0' + month;
		}
		var day = date.getDate();
		if (day < 10) {
			day = '0' + day;
		}
		if (split == null || split == '') {
			strdate = year + '年' + month + '月' + day + '日';
		} else {
			strdate = year + split + month + split + day;
		}
		return strdate;
	}
	
//格式时间的Method
	/**
	* date_time 时间  例：08:30:30
	*/
	function formatCalendarTime(date_time, h,m,s) {
	        var timeStr="";
            var hour = date_time.getHours();
            if(hour<10){
                hour = "0"+hour;
            }
            var minute = date_time.getMinutes();
            if(minute<10){
                minute = "0"+minute;
            }
            var secode = date_time.getSeconds();
            if(secode<10){
                secode = "0"+secode;
            }
            if(h==='h'){
                timeStr += hour;
            }
            if(m==='m'){
                timeStr += ":"+minute;
            }
            if(s==='s'){
                timeStr +=":"+seco
				}
		return timeStr;
	}
	

	/**
	 *周视图上一周下一周计算

	 *注释部分为计算本周开始和结束日期
	 *sign 为是上一周还是下一周的标记 - , +
	 */
	 function beforeOrAfterWeek(theDay,sign){
		//var cmonday = new Date(theDay.getTime());
		//var csunday = new Date(theDay.getTime());
		var lmonday = new Date(theDay.getTime());
		var lsunday = new Date(theDay.getTime());
		//cmonday.setDate(cmonday.getDate() - cmonday.getDay());
		//csunday.setDate(csunday.getDate() + 6 - csunday.getDay());
		if(sign == "-"){
		lmonday.setDate(lmonday.getDate() - lmonday.getDay() - 7);
		lsunday.setDate(lsunday.getDate() + 6 - lsunday.getDay() - 7);
		}else{
		lmonday.setDate(lmonday.getDate() - lmonday.getDay() +7);
		lsunday.setDate(lsunday.getDate() + 6 - lsunday.getDay() + 7);
		}
		//var current_mondayStr = formatCalendarDate(cmonday, "-");
		//var current_sundayStr = formatCalendarDate(csunday, "-");
		var last_mondayStr = formatCalendarDate(lmonday, "-");
		var last_sundayStr = formatCalendarDate(lsunday, "-");
		return last_mondayStr+","+last_sundayStr;
	 }

var easyuiPanelOnMove = function(left, top) {
	var l = left;
	var t = top;
	if (l < 1) {
		l = 1;
	}
	if (t < 1) {
		t = 1;
	}
	var width = parseInt($(this).parent().css('width')) + 14;
	var height = parseInt($(this).parent().css('height')) + 14;
	var right = l + width;
	var buttom = t + height;
	var browserWidth = $(window).width();
	var browserHeight = $(window).height();
	if (right > browserWidth) {
		l = browserWidth - width;
	}
	if (buttom > browserHeight) {
		t = browserHeight - height;
	}
	$(this).parent().css({/* 修正面板位置 */
		left : l,
		top : t
	});
};
$.fn.dialog.defaults.onMove = easyuiPanelOnMove;
$.fn.window.defaults.onMove = easyuiPanelOnMove;
$.fn.panel.defaults.onMove = easyuiPanelOnMove;